package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("新增地址:{}",addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @GetMapping("/default")
    public Result selectDefault(){
        log.info("查询默认地址 ");
        AddressBook addressBook = addressBookService.selectDefault();
        return  Result.success(addressBook);
    }

    @PutMapping
    public Result updateById(@RequestBody AddressBook addressBook){
        log.info("根据id修改地址 :{}",addressBook);
        addressBookService.updateById(addressBook);
        return Result.success();

    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
        log.info("根据id删除地址 :{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        log.info("根据id查询地址 :{}",id);
        AddressBook addressBook = addressBookService.selectById(id);
        return  Result.success(addressBook);
    }

    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址 :{}",addressBook);
        addressBookService.setDefault(addressBook.getId());
        return Result.success();
    }

}
